package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateBatchDTO;
import com.xbongbong.paas.pojo.vo.FormDataDeleteBatchVO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateBatchVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.listbatch.pojo.dto.ArchiveDTO;
import com.xbongbong.pro.listbatch.pojo.dto.CancelDTO;
import com.xbongbong.pro.listbatch.pojo.dto.CreditFreezeDTO;
import com.xbongbong.pro.listbatch.pojo.dto.CreditRevokeDTO;
import com.xbongbong.pro.listbatch.pojo.dto.DistributionDTO;
import com.xbongbong.pro.listbatch.pojo.dto.ListBatchDTO;
import com.xbongbong.saas.service.ListBatchService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/4/16 14:41
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/batch")
public class MobileListBatchController {
    @Resource
    private ListBatchService listBatchService;
    /**
     * 批量归档
     * @param archiveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/archive", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String archive(@RequestBody @Valid ArchiveDTO archiveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(archiveDTO.getSaasMark(), archiveDTO.getBusinessType(), null, DistributorMarkEnum.OTHER.getCode());
                BaseVO baseVO = listBatchService.archive(archiveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 批量退回公海池
     * @param listBatchDTO 参数
     * @param br 错误
     * @return 数据
     */
    @RequestMapping(value = "/backCustomer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String backCustomer(@RequestBody @Valid ListBatchDTO listBatchDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = listBatchService.backCustomer(listBatchDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 批量发票作废
     * @param cancelDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String cancel(@RequestBody @Valid CancelDTO cancelDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = listBatchService.cancel(cancelDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 批量删除数据
     * @param formDataDeleteBatchDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid FormDataDeleteBatchDTO formDataDeleteBatchDTO, BindingResult br) {
        XbbResponse<FormDataDeleteBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataDeleteBatchDTO.getSaasMark(), formDataDeleteBatchDTO.getBusinessType(), formDataDeleteBatchDTO.getMenuId(), formDataDeleteBatchDTO.getDistributorMark());
                FormDataDeleteBatchVO formDataDeleteBatchVO = listBatchService.deleteBySingle(formDataDeleteBatchDTO);
                //FormDataDeleteBatchVO formDataDeleteBatchVO = listBatchService.deleteBatch(formDataDeleteBatchDTO);
                response = new XbbResponse<>(formDataDeleteBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 批量更新数据
     * @param formDataUpdateBatchDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid FormDataUpdateBatchDTO formDataUpdateBatchDTO, BindingResult br) {
        XbbResponse<FormDataUpdateBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataUpdateBatchDTO.getSaasMark(), formDataUpdateBatchDTO.getBusinessType(), formDataUpdateBatchDTO.getMenuId(), formDataUpdateBatchDTO.getDistributorMark());
                FormDataUpdateBatchVO formDataUpdateBatchVO = listBatchService.updateBatch(formDataUpdateBatchDTO);
                response = new XbbResponse<>(formDataUpdateBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 批量分配
     * @param distributionDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/distribution", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String distribution(@RequestBody @Valid DistributionDTO distributionDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = listBatchService.distribution(distributionDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 信用账户冻结/解冻
     * @author xingxing.xiao
     */
    @RequestMapping(value = "/credit/freeze", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String creditFreeze(@RequestBody @Valid CreditFreezeDTO creditFreezeDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = listBatchService.creditFreeze(creditFreezeDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 临时额度撤销/重新生效
     * @author xingxing.xiao
     */
    @RequestMapping(value = "/credit/revoke", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String creditRevoke(@RequestBody @Valid CreditRevokeDTO creditRevokeDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = listBatchService.creditRevoke(creditRevokeDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
